package com.atguigu.ssyx.acl.controller;

import com.atguigu.ssyx.acl.service.PermissionService;
import com.atguigu.ssyx.commom.result.Result;
import com.atguigu.ssyx.model.acl.Permission;
import com.atguigu.ssyx.acl.service.RolePermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author ZhangYK
 * @version 8.0
 * @title PermissionController
 * @description
 * @create 2024/7/6 13:44
 */
@Api(tags = "菜单接口")
@RestController
@RequestMapping("/admin/acl/permission")
@CrossOrigin
public class PermissionController {
    @Resource
    private PermissionService permissionService;

    @Resource
    private RolePermissionService rolePermissionService;

    //1、获取权限菜单功能列表
    // url: `${api_name}`,
    //      method: 'get'
    @ApiOperation("获取权限菜单功能列表")
    @GetMapping
    public Result getPermissions() {
        List<Permission> list = permissionService.queryAllMenu();
        return Result.ok(list);
    }

    //2、删除权限
    //url: `${api_name}/remove/${id}`,
    //      method: "delete"
    @ApiOperation("递归删除菜单")
    @DeleteMapping("remove/{id}")
    public Result removePermission(@PathVariable Long id) {
        permissionService.removeChildById(id);
        return Result.ok(null);
    }

    //3、保存权限
    //url: `${api_name}/save`,
    //      method: "post",
    //      data: permission
    @ApiOperation("保存权限")
    @PostMapping("sava")
    public Result savePermission(@RequestBody Permission permission) {
        boolean flag = permissionService.save(permission);
        if (flag) {
            return Result.ok(null);
        }
        return Result.fail(null);
    }


    //4、修改权限
    //url: `${api_name}/save`,
    //      method: "post",
    //      data: permission
    @ApiOperation("修改权限")
    @PutMapping("/sava")
    public Result updatePermission(@RequestBody Permission permission) {
        boolean flag = permissionService.save(permission);
        if (flag) {
            return Result.ok(null);
        }
        return Result.fail(null);
    }


    @ApiOperation(value = "根据角色获取菜单数据")
    @GetMapping("/toAssign/{roleId}")
    public Result toAssign(@PathVariable Long roleId) {
        Map<String, Object> roleMap = permissionService.findPermissionByRoleId(roleId);
        return Result.ok(roleMap);
    }

    @ApiOperation(value = "根据用户分配角色")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestParam Long roleId,@RequestParam Long[] permissionId) {
        permissionService.saveRolePermissionRealtionShip(roleId,permissionId);
        return Result.ok(null);
    }
}
